<template>
  <div>
    <el-row style="margin: 20px">
      <el-button type="primary" @click="addStu">手动添加</el-button>
      <el-button type="success" @click="agreeAll">同意全部</el-button>
      <el-button type="info" @click="rejectAll">否决全部</el-button>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}"
    >
      <el-table-column label="姓名">
        <template slot-scope="scope">
          <span>{{ scope.row.etmsUser.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门">
        <template slot-scope="scope">
          <span>{{ scope.row.etmsUser.dept.deptName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职位">
        <template slot-scope="scope">
          <span>{{ scope.row.etmsUser.userPosition }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱">
        <template slot-scope="scope">
          <span>{{ scope.row.etmsUser.userEmail }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.applyStatus == '0'">待同意</span>
          <span v-if="scope.row.applyStatus == '1'">已同意</span>
          <span v-if="scope.row.applyStatus == '-1'">已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div v-if="scope.row.applyStatus == '0'">
            <el-button size="mini" @click="handleAgree(scope.$index, scope.row)">同意</el-button>
            <el-button size="mini" type="danger" @click="handleReject(scope.$index, scope.row)">拒绝</el-button>
          </div>
          <div v-if="scope.row.applyStatus != '0'">
            <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage4"
      :page-sizes="[2, 5, 8, 10]"
      :page-size="formInLine.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 30px; margin-bottom: 20px; text-align: center;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="新增学员" :visible.sync="dialogFormVisible">
      <el-form :model="stuTable" :rules="rules">
        <el-form-item label="学员姓名" :label-width="formLabelWidth" prop="userName">
          <el-select v-model="stuTable.userId" placeholder="请选择学员">
            <span v-for="user in userList" :key="user.index">
              <el-option :label="user.userName" :value="user.userId"></el-option>
            </span>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="add">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllStudent } from '@/api/item'
import { agree } from '@/api/item'
import { reject } from '@/api/item'
import { deleteStu } from '@/api/item'
import { Message } from 'element-ui'
import { findAllUser } from '@/api/user'
import { addStu } from '@/api/student'

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      formLabelWidth: '80px',
      stuTable: {
        itemId: this.$route.query.itemId
      },
      userList: [],
      formInLine: {
        page: 1,
        limit: 5,
        id: 0
      },
      rules: {
        userName : {
          required: true,
          message: "内容不为空",
          trigger: "blur",
        }
      }
    }
  },
  created() {
    this.formInLine.id = this.$route.query.itemId
    this.getStudent(this.formInLine)
  },
  methods: {
    getStudent(data) {
      getAllStudent(data).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
      }).catch((err) => {

      })
    },

    // 报名状态操作
    // 同意
    handleAgree(index, row) {
      // console.log(index, row);
      agree(row.studentId).then((res) => {
        this.getStudent(this.formInLine)
      }).catch((err) => {

      })
    },
    // 拒绝
    handleReject(index, row) {
      // console.log(index, row);
      reject(row.studentId).then((res) => {
        this.getStudent(this.formInLine)
      }).catch((err) => {

      })
    },
    // 删除
    handleDelete(index, row) {
      // console.log(index, row);
      deleteStu(row.studentId).then((res) => {
        this.getStudent(this.formInLine)
      }).catch((err) => {
        
      })
    },
    // 同意全部
    agreeAll() {
      // console.log(index, row);
      agree(0).then((res) => {
        this.getStudent(this.formInLine)
      }).catch((err) => {
        console.log(error)
      })
    },
    // 否决全部
    rejectAll() {
      // console.log(index, row);
      reject(0).then((res) => {
        this.getStudent(this.formInLine)
        Message({
          message: res.message || 'success',
          type: 'success',
          duration: 2 * 1000
        })
      }).catch((err) => {

      })
    },

    //手动新增
    addStu(){
      findAllUser().then((res) => {
        this.userList = res.data
      }).catch((err) => {
      });
      this.dialogFormVisible = true;
    },
    add(){
      addStu(this.stuTable).then((res) => {
        Message({
          message: res.message || 'success',
          type: 'success',
          duration: 2 * 1000
        });
        this.getStudent(this.formInLine);
        this.dialogFormVisible = false;
      }).catch((err) => {
        
      });
    },
    cancel(){
      this.dialogFormVisible = false;
      this.getStudent(this.formInLine);
    },

    handleSizeChange(val) {
      this.formInLine.limit = val
      this.getStudent(this.formInLine)
    },
    handleCurrentChange(val) {
      this.formInLine.page = val
      this.getStudent(this.formInLine)
    }
  }
}
</script>
